/*子应用*/
const express = require("express")

const query = require("../pool")

const vertify = require("../modules/token")

const material = express.Router()

// 获取用户数据
material.get("/all", async (req, res) => {
  let obj = req.query
  // console.log("12", obj)
  let uid = vertify.verifytoken(obj[0])
  let result = await query(
    `select uname,phone,gender,user_icon from cake_user where uid=${uid.uid}`
  )
  if (result.length == 0) {
    res.send({ code: 0, msg: "未查询到相关结果" })
  } else {
    res.send({ code: 1, msg: "搜索成功", data: result })
  }
})

// 更新数据
material.get("/update", async (req, res) => {
  let obj = req.query
  // console.log(obj)
  let uid = vertify.verifytoken(obj.token)
  // console.log(uid.uid)
//console.log(obj.radio)
  let rul = await query(
    `update cake_user set uname='${obj.uname}',phone='${obj.phone}',gender='${obj.radio}',user_icon='${obj.icon}' where uid='${uid.uid}'`
  )
  if (rul.changedRows == 0) {
    res.send({ code: 0, msg: "失败" })
  } else {
    res.send({ code: 1, msg: "成功" })
  }
})

//暴露
module.exports = material
